package com.qf.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.TaxReturn;
import com.qf.entity.TotalSales;
import com.qf.service.TotalSalesService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/total/user")
public class TotalSalesController {

    @Autowired
    private TotalSalesService service;
    @PostMapping("/login")
    public R login(@RequestBody TotalSales totalSales, HttpSession session){
        System.out.println("TotalSalesController.login,接收数据"+totalSales );
        TotalSales login = service.login(totalSales);
        if (login != null) {
            //登录成功，存储用户信息
            session.setAttribute("userInfo",login);
            //造假token，给前端
            HashMap<String,String> map = new HashMap<>();
            map.put("token", UUID.randomUUID().toString());
            return R.ok(map);
        }
        return R.fail();
    }

    @GetMapping("/info")
    public R login(HttpSession session){
        TotalSales info = (TotalSales) session.getAttribute("userInfo");
        return R.ok(info);

    }

    /* 退出系统 */
    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();
        return R.ok();
    }

    /*查询全部*/
    @GetMapping("/list")
    public R findAll(@RequestParam HashMap<String,String> map){
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(pageNum,pageSize);

        List<TotalSales> list = service.findAll(map);
        PageInfo<TotalSales> info = new PageInfo<>(list);
        return R.ok(info);
    }

    /*关键字查询*/
    @GetMapping("/list1")
    public R findAll1(){

        List<String> list = service.findAll1();
        return R.ok(list);
    }

    /*通过id删除*/
    @GetMapping("/del")
    public R deleteById(int id) {
        boolean isok = service.deleteById(id);
        System.out.println(isok);

        return R.ok(isok);
    }

    /*添加*/
    @PostMapping("/add")
    public R addTotalSales(@RequestBody TotalSales totalSales) {
        boolean isok = service.addTotalSales(totalSales);

        return R.ok(isok);
    }

    /*批量删除*/
    @GetMapping("/delBatch")
    public R delBatch(@RequestParam List<Integer> ids){
        boolean isok = service.delBatch(ids);
        System.out.println("TotalSalesController.delBatch,接收到的ids = " +ids );
        return R.ok(isok);
    }

    /*更正*/
    @PostMapping("/change")
    public R change(@RequestBody TotalSales totalSales) {
        boolean isok = service.change(totalSales);
        return R.ok(isok);
    }

}
